package ca.tatham.senssim.util;

public class Address
{
  public static final Address BROADCAST = new Address(-1, -1, -1, -1);
  private final int m_address;

  public Address(int i, int j, int k, int l)
  {
    m_address = i << 24 | j << 16 | k << 8 | l;
  }

  public int sizeOf()
  {
    return 4;
  }

  @Override
  public String toString()
  {
    return Integer.toString(m_address >>> 24) + '.'
        + Integer.toString(m_address >> 16 & 0x00FF) + '.'
        + Integer.toString(m_address >> 8 & 0x00FF) + '.'
        + Integer.toString(m_address & 0x000000FF);
  }

  /**
   * 
   * @param address
   * @return true if the given address is "destined" for this address
   */
  public boolean matches(Address address)
  {
    return address == BROADCAST || address.equals(this);
  }

  @Override
  public boolean equals(Object obj)
  {
    Address that = (Address) obj;
    return this.m_address == that.m_address;
  }

  @Override
  public int hashCode()
  {
    return m_address;
  }

  public boolean isBroadcast()
  {
    return BROADCAST.equals(this);
  }

}
